<?php

namespace App\Http\Controllers\Api\V1\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class CouponRecordsResource extends JsonResource
{	

	 public static function secondsToHMS($seconds) {
        // 验证输入是否为非负整数
        if (!is_numeric($seconds) || $seconds < 0) {
            throw new InvalidArgumentException("秒数必须为非负整数");
        }
        
        // 确保为整数
        $seconds = (int)$seconds;
        
        // 计算小时、分钟、秒
        $hours = floor($seconds / 3600);
        $minutes = floor(($seconds % 3600) / 60);
        $seconds = $seconds % 60;
        
        // 格式化为两位数
        return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
    }
    public function toArray($request)
    {
    	$resource = $this->resource;
    	
    	$destory_at = "00:00:00";
    	if($resource->status =="unused"){

    		if(strtotime((string)$resource->expired_at) > time()){
    			$destory_at= strtotime((string)$resource->expired_at)- time();

    			$destory_at = $this->secondsToHMS($destory_at);
    		}
    		
    	}
        return [
        	'id' => (int)$resource->id,
        	'phone' => Str::mask($resource->user->phone, '*', 3, 4),
	        'code' => $resource->code,
	        'coupon' => $resource->coupon,
	        'status' => (string)$resource->status,
	        'used_at' => (string)$resource->used_at,
	        'destory_at'=>$destory_at,
	        'created_at' => (string)$resource->created_at,
        ];
    }
}
